package com.pinla.common.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;


/**
 * @author whk 2009-10-4 19PAY 有限公司 2009--2019 session验证以及用户轨迹
 */
public class SessionValidFilter implements Filter {
	protected FilterConfig config;
	private String loginpage;
	private String reloginpage;
	private String noHigherAgentRightPage;
	private List<String> notCheckURLList = new ArrayList<String>();
	private List<String> doCheckHigherAgentURLList = new ArrayList<String>();
	protected static final Logger logger = Logger.getLogger(SessionValidFilter.class);
	private static final String LOGIN_USER = "loginUser";

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();

		String explorerInfo = "user-agent:"+req.getHeader("user-agent");
		String requestpath = req.getServletPath();
		
		chain.doFilter(req, res);
	}
	
	private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {
		String uri = request.getServletPath();
		return notCheckURLList.contains(uri);
	}
	
	private boolean checkRequestURIInDoCheckHigherAgentURLList(HttpServletRequest request) {
		String uri = request.getServletPath();
		return doCheckHigherAgentURLList.contains(uri);
	}

	/**
	 * 初始化
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		this.config = filterConfig;
		loginpage = config.getInitParameter("loginPage");
		reloginpage = config.getInitParameter("reloginPage");
		noHigherAgentRightPage = config.getInitParameter("noHigherAgentRightPage");
		String notCheckURLListStr = filterConfig
				.getInitParameter("notCheckURLList");
		String doCheckHigherAgentURLListStr = filterConfig.getInitParameter("doCheckHigherAgentURLList");

		if (notCheckURLListStr != null) {
			StringTokenizer st = new StringTokenizer(notCheckURLListStr, ",");
			notCheckURLList.clear();
			while (st.hasMoreTokens()) {
				notCheckURLList.add(st.nextToken().trim());
			}
		}
		
		if (doCheckHigherAgentURLListStr != null) {
			StringTokenizer st = new StringTokenizer(doCheckHigherAgentURLListStr, ",");
			doCheckHigherAgentURLList.clear();
			while (st.hasMoreTokens()) {
				doCheckHigherAgentURLList.add(st.nextToken().trim());
			}
		}
	}
	
	/**
	 * 结束方法
	 */
	public void destroy() {
		notCheckURLList.clear();
		doCheckHigherAgentURLList.clear();
	}
}